package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionServive;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Stream;

@RestController
@RequestMapping(path = "/permission")
public class PermissionController {

    @Reference
    private PermissionServive permissionServive;

    @GetMapping("/all")
    public Result findAll() {
        List<Permission> list=null;
        //分页查询权限信息
        try {
             list=  permissionServive.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @GetMapping
    public Result findByPage(QueryPageBean queryPageBean) {
        //分页查询权限信息
        try {
            PageResult pageResult = permissionServive.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        // 弹出编辑窗口,根据ID查询权限详情
        try {
            Permission permission = permissionServive.findById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @PutMapping()
    public Result updatePermission(@RequestBody Permission permission) {
        // 修改权限信息
        try {
            permissionServive.updatePermission(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    @PostMapping()
    public Result addPermission(@RequestBody Permission permission) {
        // 新增权限信息
        try {
            permissionServive.addPermission(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    @DeleteMapping("/{id}")
    public Result deletePermission(@PathVariable("id") Integer id) {
        // 删除权限信息
        try {
            permissionServive.deletePermission(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }


    //查询菜单关联的权限
    //  axios.get("/permission/menu/association?menuIds=" + this.menuIds)
    @GetMapping("/menu/association")
    public Result selectAssociation(String menuIds) {
        String[] split = menuIds.split(",");
        Integer[] integers = Stream.of(split).map(ids -> Integer.valueOf(ids)).toArray(Integer[]::new);
        List<Permission> permissions = permissionServive.findMenuPermission(integers);
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissions);
    }
}

